<?php
/**
 * Smart Library - Smart Framework
 * http://smart-framework.highet.eu
 *
 * @category   SmartL
 * @package    SmartL_DatabaseManager
 * @copyright  Copyright (c) 2005-2008 Smart Framework team - Stepan Sindelar, Vojtech Vit
 * @license    http://www.opensource.org/licenses/bsd-license.php	New BSD License
 */

/**
 * @see SmartL_DatabaseManager_Interface
 */
require_once 'SmartL/DatabaseManager/Interface.php';

/**
 * Implementation of Database manager for Doctrine ORM.
 *
 * @package SmartL_DatabaseManager
 */
class SmartL_DatabaseManager_Doctrine implements SmartL_DatabaseManager_Interface {
	/**
	 * @var SmartL_DatabaseManager_Doctrine
	 */
	private static $_instance = null;
	/**
	 * @var Doctrine_Manager
	 */
	private $_doctrineManager = null;
	
	protected function __construct() {
	}
	
	/**
	 * Opend doctrine connection.
	 * Implementation of function from interface SmartL_DatabaseManager_Interface.
	 *
	 * @param string $name
	 * @param array $options This adapter needs only one option->connectionString.
	 * @return Doctrine_Connection
	 */
	public function openDbConnection($name, $options) {
		if (! isset ( $options ['connectionString'] )) {
			require_once 'SmartL/DatabaseManager/Exception.php';
			throw new SmartL_DatabaseManager_Exception ( "Value of connectionString in " . "options for method openDbConnection of SmartL_DatabaseManager_Doctrine missing" );
		}
		$connection = $this->getDoctrineManager ()->openConnection ( $options ['connectionString'], $name );
		
		if (class_exists ( 'SmartL_Profiler' ) and SmartL_Profiler::getInstance ()->isEnabled ()) {
			$profiler = SmartL_Profiler::getInstance ();
			$doctrineProfiler = $profiler->getCustomProfiler ( 'doctrine' );
			if ($doctrineProfiler === null) {
				require_once 'SmartL/Doctrine/Profiler.php';
				$doctrineProfiler = new SmartL_Doctrine_Profiler ( );
				$profiler->addCustomProfiler ( 'doctrine', $doctrineProfiler );
			}
			$doctrineProfiler->addConnection ( $connection );
		}
		
		return $connection;
	}
	
	/**
	 * Sets current connection name for Doctrine.
	 *
	 * @param string $name Name of current connection.
	 */
	public function setCurrentConnection($name) {
		$this->getDoctrineManager ()->setCurrentConnection ( $name );
	}
	
	/**
	 * @return string Name of current connection.
	 */
	public function getCurrentConnectionName() {
		$manager = $this->getDoctrineManager ();
		return $manager->getConnectionName ( $manager->getCurrentConnection () );
	}
	
	/**
	 * Rteurns connection object with given name.
	 *
	 * @param string $name
	 */
	public function getConnection($name) {
		return $this->getDoctrineManager ()->getConnection ( $name );
	}
	
	/**
	 * Returns "Doctrine"
	 * Implementation of function from interface SmartL_DatabaseManager_Interface.
	 * @return string "Doctrine"
	 */
	public function getLibraryName() {
		return "Doctrine";
	}
	
	/**
	 * @return Doctrine_Manager
	 */
	public function getDoctrineManager() {
		if ($this->_doctrineManager == null) {
			$this->_doctrineManager = Doctrine_Manager::getInstance ();
		}
		return $this->_doctrineManager;
	}
	
	/**
	 * Sets Doctrine manager.
	 *
	 * It could be used for tests purpouse,
	 * normally if this function wasn't called,
	 * the manager is got trought Doctrine_Manager::getInstance().
	 *
	 * @param Doctrine_Manager $manager
	 * @deprecated
	 */
	public function setDoctrineManager($manager) {
		$this->_doctrineManager = $manager;
	}
	
	/**
	 * Implementation of singleton pattern.
	 *
	 * @return SmartL_DatabaseManager_Doctrine
	 */
	public static function getInstance() {
		if (self::$_instance == null) {
			self::$_instance = new SmartL_DatabaseManager_Doctrine ( );
		}
		return self::$_instance;
	}
}